package com.cy.milktea.common.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class RunTimeAspect {

    @Pointcut("@annotation(com.cy.milktea.common.annotation.RunTime)")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println(6666);
        long start = 0;
        long end = 0;
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        String methodStr = signature.getMethod().toString();
        try {
            start = System.currentTimeMillis();
            Object result = pjp.proceed();
            end = System.currentTimeMillis();
            log.info("method: {} ,run ok, run Time:{} ", methodStr, end - start);
            return result;
        } catch (Throwable e) {
            long stop = System.currentTimeMillis();
            log.error("method: {} , run error, runTime: {} ", methodStr, end - stop);
            throw e;
        }
    }
}
